﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Entity;
using System.Data.SqlClient;
using System.Data;

namespace Business.Dal
{
    public class KategoriProvider : Provider<Kategori>
    {
        public override bool Insert(Kategori tObject)
        {
            bool retunValue = false;

            SqlCommand cmd = new SqlCommand("KategoriEkle");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@KategoriAdi", SqlDbType.NVarChar).Value = tObject.KategoriAdi;
            cmd.Parameters.Add("@UstKategoriID", SqlDbType.Int).Value = tObject.UstKategoriID;
            cmd.Parameters.Add("@TumKategoriID", SqlDbType.VarChar).Value = tObject.TumKategoriID;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            tObject.KategoriID = Convert.ToInt32(cmd.ExecuteScalar());
            retunValue = true;
            return retunValue;

        }

        public override bool Update(Kategori tObject)
        {
            bool retunrValue = false;

            SqlCommand cmd = new SqlCommand("KategoriGuncelle");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();

            cmd.Parameters.Add("@KategoriID", SqlDbType.Int).Value = tObject.KategoriID;
            cmd.Parameters.Add("@UstKategoriID", SqlDbType.Int).Value = tObject.UstKategoriID;
            cmd.Parameters.Add("@KategoriAdi", SqlDbType.NVarChar).Value = tObject.KategoriAdi;
            cmd.Parameters.Add("@TumKategoriID", SqlDbType.VarChar).Value = tObject.TumKategoriID;
            retunrValue = Convert.ToBoolean(cmd.ExecuteNonQuery());

            return retunrValue;
        }

        public override bool Delete(int RecordID)
        {
            bool returnValue = false;
            SqlCommand cdm = new SqlCommand("KategoriSil");
            cdm.CommandType = CommandType.StoredProcedure;
            cdm.Connection = Tools.Connection();
            Tools.Open();
            cdm.Parameters.Add("@KategoriID", SqlDbType.Int).Value = RecordID;
            returnValue = Convert.ToBoolean(cdm.ExecuteNonQuery());
            return returnValue;
        }

        public List<Kategori> GetAllRows()
        {
            List<Kategori> retunValue = new List<Kategori>();
            SqlCommand cmd = new SqlCommand("KategorileriGetir");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            SqlDataReader dr;
            Tools.Open();
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                Kategori K = new Kategori();
                K.KategoriID = dr["KategoriID"] is DBNull ? 0 : Convert.ToInt32(dr["KategoriID"]);
                K.UstKategoriID = dr["UstKategoriID"] is DBNull ? 0 : Convert.ToInt32(dr["UstKategoriID"]);
                K.KategoriAdi = dr["KategoriAdi"] is DBNull ? String.Empty : dr["KategoriAdi"].ToString();
                K.TumKategoriID = dr["TumKategoriID"] is DBNull ? String.Empty : dr["TumKategoriID"].ToString();
                retunValue.Add(K);
            }
            
            dr.Dispose();
            return retunValue;
        }

        public Kategori GetKategori(string RecordID)
        {
            SqlCommand cmd = new SqlCommand("KategoriGetir");
            cmd.Parameters.AddWithValue("@KategoriID", RecordID);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
           
            Tools.Open();
            SqlDataReader dr= cmd.ExecuteReader();
            
            dr.Read();
            Kategori K = new Kategori();
            K.KategoriID = dr["KategoriID"] is DBNull ? 0 : Convert.ToInt32(dr["KategoriID"]);
            K.UstKategoriID = dr["UstKategoriID"] is DBNull ? 0 : Convert.ToInt32(dr["UstKategoriID"]);
            K.KategoriAdi = dr["KategoriAdi"] is DBNull ? String.Empty : dr["KategoriAdi"].ToString();
            K.TumKategoriID = dr["TumKategoriID"] is DBNull ? String.Empty : dr["TumKategoriID"].ToString();
            
            dr.Dispose();
            return K;
        }

        public List<Kategori> GetUstKategoriler(int RecordID)
        {
            List<Kategori> retunValue = new List<Kategori>();
            SqlCommand cmd = new SqlCommand("UstKategoriGetir");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            cmd.Parameters.AddWithValue("@UstKategoriID", RecordID);
            Tools.Open();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Kategori K = new Kategori();
                K.KategoriID = dr["KategoriID"] is DBNull ? 0 : Convert.ToInt32(dr["KategoriID"]);
                K.UstKategoriID = dr["UstKategoriID"] is DBNull ? 0 : Convert.ToInt32(dr["UstKategoriID"]);
                K.KategoriAdi = dr["KategoriAdi"] is DBNull ? String.Empty : dr["KategoriAdi"].ToString();
                K.TumKategoriID = dr["TumKategoriID"] is DBNull ? String.Empty : dr["TumKategoriID"].ToString();
                retunValue.Add(K);             
            }

            dr.Dispose();
            return retunValue;

        }
    }
}
